/*
 * csMaker.cpp
 *
 *  Created on: 2012-10-8
 *      Author: administrator
 */
#include <sys/stat.h>
#if defined( __MINGW32__ ) || defined( __MINGW64__ )
#include <io.h>
#endif
#include "csWriter.hpp"
#include "../writer.hpp"










static int writeInfo( ProtocolModule *module , char *dirPath ) {
	char* pWrite = workBuff ;
	char infoPath[256] ;
	FILE *infoFile ;
	ProtocalType *type ;
	ProtocalRequester *request ;
	ProtocalRespond *respond ;

	sprintf( infoPath , "%s/%sInfo.cs" , dirPath , module->name ) ;
	infoFile = fopen( infoPath , "wb" ) ;

	pWrite += sprintf( pWrite ,
			"// 简    述：%s\n"
			"// 创建标志：%s\n"
			"// 修改标志：\n" ,
			module->summary , module->creator ) ;
	for( ProtocolModify *modify = module->modifies.next ; modify ; modify = modify->next ) {
		pWrite += sprintf( pWrite , "//		%s\n" , modify->modify ) ;
	}
	pWrite += sprintf( pWrite , "\n"
			"using System ;\n"
			"\n"
			"namespace Weedong.BX\n"
			"{\n\n" ) ;
	for( type = module->types ; type ; type = type->next ) {
		pWrite += sprintf( pWrite , "\tpublic class %s\n"
				"\t{\n"
				"%s\n"
				"\t}\n" ,
				type->name , type->csContent ) ;
	}
	for( request = module->requesters ; request ; request = request->next ) {
		pWrite += sprintf( pWrite , "\tpublic class %s\n"
				"\t{\n"
				"%s\n"
				"\t}\n" ,
				request->name , request->csContent ) ;
	}
	for( respond = module->responds ; respond ; respond = respond->next ){
		pWrite += sprintf( pWrite , "\tpublic class %s\n"
				"\t{\n"
				"%s\n"
				"\t}\n" ,
				respond->name , respond->csContent ) ;
	}
	writeToFile( infoFile , workBuff , pWrite - workBuff ) ;
	writeToFile( infoFile , "}\n" , 2 ) ;
	fclose( infoFile ) ;
	return 0 ;
}
static int writeReceiver( ProtocolModule *module , char *dirPath ) {
	char *pWrite ;
	char receiverPath[256] ;
	FILE *receiverFile ;
	ProtocalRespond *respond ;
	char receiverHeader[] = "using System;\n"
			"using System.Collections.Generic;\n"
			"namespace Weedong.BX{\n"
			"	public class %sReceiver:ReceiveAction\n"
			"	{\n"
			"		private static %sReceiver instance ;\n"
			"		public static %sReceiver getInstance() \n"
			"		{\n"
			"			if( instance == null ) {\n"
			"				instance = new %sReceiver() ;\n"
			"			}\n"
			"			return instance ;\n"
			"		}\n\n" ;

	sprintf( receiverPath , "%s/%sReceiver.cs" , dirPath , module->name ) ;
	receiverFile = fopen( receiverPath , "wb" ) ;
	pWrite = workBuff ;
	/* 单例通用代码 */
	pWrite += sprintf( pWrite , receiverHeader ,
			module->name ,
			module->name ,
			module->name ,
			module->name ) ;

	/* 消息注册函数 */
	pWrite += sprintf( pWrite ,
			"		public override List<int> ListHandleMsg() \n"
			"		{\n"
			"			List<int> mylist = new List<int>() ;\n" ) ;
	for( respond = module->responds ; respond ; respond = respond->next ) {
		pWrite += sprintf( pWrite ,
				"			mylist.Add( ServerProtocolDef.%s);\n" ,
				respond->notify ) ;
	}
	pWrite += sprintf( pWrite ,
			"			return mylist ;\n"
			"		}\n\n" ) ;

	/* 消息映射函数 */
	pWrite += sprintf( pWrite ,
			"		public override void MsgHandler( Package np )\n"
			"		{\n"
			"			switch( np.code ) { \n" ) ;
	for( respond = module->responds ; respond ; respond = respond->next ) {
		pWrite += sprintf( pWrite ,
				"			case ServerProtocolDef.%s :{ \n"
				"				%s info = new %s() ;\n"
				"				info.unSerialize( np ) ; \n"
				"				SendNotification( NetDef.%s , info ) ; \n"
				"				break ; \n"
				"			} \n" ,
				respond->notify ,
				respond->name , respond->name ,
				respond->notify ) ;
	}
	pWrite += sprintf( pWrite ,
			"			}\n"
			"		}\n"
			"	}\n"
			"}\n\n" ) ;
	writeToFile( receiverFile , workBuff , pWrite - workBuff ) ;
	fclose( receiverFile ) ;
	return 0 ;
}
static int writeSender( ProtocolModule *module , char *dirPath ) {
	char *pWrite ;
	char senderPath[256] ;
	FILE *senderFile ;
	ProtocalRequester *request ;
	char senderHeader[] =
			"using System ;\n"
			"namespace Weedong.BX\n"
			"{\n"
			"	public class %sSender\n"
			"	{\n\n" ;
	sprintf( senderPath , "%s/%sSender.cs" , dirPath , module->name ) ;
	senderFile = fopen( senderPath , "wb" ) ;
	pWrite = workBuff ;

	pWrite += sprintf( pWrite , senderHeader , module->name ) ;
	for( request = module->requesters ; request ; request = request->next ) {
		pWrite += sprintf( pWrite ,
				"		public static void %s( %s info ) {\n"
				"			Package np = new Package() ;\n"
				"			np.write( (int)0 ) ;\n"
				"			np.write( ClientProtocolDef.%s ) ;\n"
				"			info.serialize( np ) ;\n"
				"			Main.GameNet.SendPackage( np ) ;\n"
				"		}\n" ,
				request->name , request->name ,
				request->notify ) ;
	}
	pWrite += sprintf( pWrite ,
			"	}\n"
			"}\n" ) ;
	writeToFile( senderFile , workBuff , pWrite - workBuff ) ;
	fclose( senderFile ) ;
	return 0 ;
}

int writeCsModule( ProtocolModule *module ) {
	char dirPath[256] ;

	sprintf( dirPath , "%s/%s/" , cSharpDirPath , module->name ) ;
#if defined( __MINGW32__ ) || defined( __MINGW64__ )
	mkdir( dirPath ) ;
#else
	mkdir( dirPath , S_IREAD | S_IWRITE | S_IEXEC| S_IRGRP | S_IWGRP | S_IXGRP ) ;
#endif

	writeInfo( module , dirPath ) ;
	writeReceiver( module , dirPath ) ;
	writeSender( module , dirPath ) ;
	return 0 ;
}

int writeCsGlobal( ProtocolModule** modules , int iModule ) {
	int i ;
	char *pWrite = workBuff ;
	FILE *notifyFile ;
	FILE *protocolFile ;
	ProtocalRespond *respond ;
	ProtocalRequester *request ;

	notifyFile = fopen( CSharpNotifyDefPath , "wb" ) ;
	protocolFile = fopen( CSharpProtoDefPath , "wb" ) ;

	/* 写 notify 文件 */
	pWrite += sprintf( pWrite ,
			"using System ;\n"
			"using System.Collections.Generic ; \n"
			"using System.Linq ;\n"
			"using System.Text ;\n"
			"using PureMVC.Patterns ;\n"
			"using PureMVC.Interfaces ;\n"
			"\n"
			"namespace Weedong.BX\n"
			"{\n"
			"	public class NetDef\n"
			"	{\n" ) ;
	for( i = 0 ; i < iModule ; ++i ) {
		for( respond = modules[i]->responds ; respond ; respond = respond->next ) {
			pWrite += sprintf( pWrite ,
					"		public const string %s = \"NETMSG_%s\" ; \n" ,
					respond->notify , respond->notify ) ;
		}
		pWrite += sprintf( pWrite , "\n" ) ;
	}
	pWrite += sprintf( pWrite ,
			"	}\n"
			"}\n" ) ;
	writeToFile( notifyFile , workBuff , pWrite - workBuff ) ;

	/* 写 protocolDef 文件 */
	pWrite = workBuff ;
	pWrite += sprintf( pWrite ,
			"using System ;\n"
			"using System.Collections.Generic ; \n"
			"using System.Linq ;\n"
			"using System.Text ;\n"
			"using PureMVC.Patterns ;\n"
			"using PureMVC.Interfaces ;\n"
			"\n"
			"namespace Weedong.BX\n"
			"{\n"
			"	public class ClientProtocolDef \n"
			"	{\n" ) ;
	// 请求消息号
	for( i = 0 ; i < iModule ; ++i ) {
		for( request = modules[i]->requesters ; request ; request = request->next ) {
			pWrite += sprintf( pWrite ,
					"		public const int %s = 0x%04x%04x ; \n" ,
					request->notify , modules[i]->id , request->id ) ;
		}
		pWrite += sprintf( pWrite , "\n" ) ;
	}
	pWrite += sprintf( pWrite , "	}\n\n" ) ;

	// 应答消息号
	pWrite += sprintf( pWrite ,
			"	public class ServerProtocolDef \n"
			"	{ \n" ) ;
	for( i = 0 ; i < iModule ; ++i ) {
		for( respond = modules[i]->responds ; respond ; respond = respond->next ) {
			pWrite += sprintf( pWrite ,
					"		public const int %s = 0x%04x%04x ; \n" ,
					respond->notify , modules[i]->id , respond->id ) ;
		}
		pWrite += sprintf( pWrite , "\n" ) ;
	}
	pWrite += sprintf( pWrite ,
			"	}\n"
			"}\n" ) ;
	writeToFile( protocolFile , workBuff , pWrite - workBuff ) ;

	fclose( notifyFile ) ;
	fclose( protocolFile ) ;
	return 0 ;
}
































